package com.hznc.wcs.util;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;


/**
 * 金额转换
 */
public class NumberToRMBUtils {

    /**
     * 整数部分单位
     */
    static String[] cashUnitLeft = {"元", "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟", "万"};
    /**
     * 小数部分单位
     */
    static String[] cashUnitRight = {"角", "分", "厘"};
    /**
     * 大写数字
     */
    static String[] upperNumber = {"零", "壹", "贰", "弎", "肆", "伍", "陆", "柒", "捌", "玖"};


    /**
     * 移除所有BigDecimal的废弃补0
     *
     * @param money the money
     * @return the big decimal
     */
    public static BigDecimal removeAmtLastZero(BigDecimal money) {
        String strMoney = money.toString();
        if (strMoney.indexOf('.') != -1) {
            String[] arr = strMoney.split("\\.");
            String strDecimals = arr[1];
            List<String> list = new ArrayList<>();
            boolean isCanAdd = false;
            for (int i = strDecimals.length() - 1; i > -1; i--) {
                String ss = String.valueOf(strDecimals.charAt(i));
                if (!ss.equals("0")) {
                    isCanAdd = true;
                }
                if (!ss.equals("0") || isCanAdd) {
                    list.add(ss);
                }
            }
            StringBuilder strZero = new StringBuilder();
            for (int i = list.size() - 1; i > -1; i--) {
                strZero.append(list.get(i));
            }
            strMoney = String.format("%s.%s", arr[0], strZero);
        }
        return new BigDecimal(strMoney);
    }

    /**
     * 金额转换
     *
     * @param money the money
     * @return the string
     */
    public static String convert(BigDecimal money) {

        String moneyString = money.toString();
        try {
            if (moneyString.contains(".")) {                        //把数值分为整数型和带小数的数值分开处理。
                String left = moneyString.substring(0, moneyString.indexOf("."));
                String right = moneyString.substring(moneyString.indexOf(".") + 1);
                return convertLeft(left) + convertRight(right);
            } else {
                return convertLeft(moneyString);
            }
        } catch (IllegalArgumentException e) {
            System.out.println(e.getMessage());
            System.out.println("请输入转换范围内现金");
            return null;
        }
    }

    /**
     * 整数部分金额转换
     *
     * @param left the money
     * @return the string
     */
    private static String convertLeft(String left) {
        //根据单位'亿','万','元'，把整数部分分为3种情况处理
        int length = left.length();
        //金额在千元以内
        if (length <= 4) {
            //金额为'0'元时的特殊情况
            if (length == 1 && Integer.parseInt(left) == 0) {
                return "零元";
            }
            return convertPart(left, length);
        } else if (length <= 8) {
            //金额在千万元以内
            String part1 = left.substring(0, length - 4);
            String part2 = left.substring(length - 4, length);
            String result1 = convertPart(part1, length);
            String result2 = convertPart(part2, 4);

            return result1 + result2;
        } else if (length <= 12) {
            //金额在千亿元以内
            String part1 = left.substring(0, length - 8);            //截取单位为'亿'部分数值。
            String part2 = left.substring(length - 8, length - 4);    //截取单位为'万'部分数值。
            String part3 = left.substring(length - 4, length);        //截取单位为'元'部分数值。

            String result1 = convertPart(part1, length);            //转换单位为'亿'部分数值。
            String result2 = convertPart(part2, 8);                    //转换单位为'万'部分数值。
            String result3 = convertPart(part3, 4);                    //转换单位为'元'部分数值。

            String result = result1 + result2 + result3;
            return result;
        } else {
            throw new IllegalArgumentException("超出转换数值范围！");
        }

    }

    /**
     * 小部分金额转换
     *
     * @param right the money
     * @return the string
     */
    private static String convertRight(String right) {
        String result = "";
        for (int i = 0; i < right.length(); i++) {
            String detail = right.substring(i, i + 1);
            int value = Integer.parseInt(detail);
            String number = upperNumber[value];
            String unit = cashUnitRight[i];
            result = result.concat(number).concat(unit);
        }
        result = result.replaceAll("零角", "").replaceAll("零分", "").replaceAll("零厘", "");
        return result;
    }

    /**
     * 具体金额转换
     *
     * @param part     the money
     * @param position the money
     * @return the string
     */
    private static String convertPart(String part, int position) {
        String result = "";
        //用于排除单位为'万'时，四位数值均为'0'的情况
        if (part.equals("0000") && position == 8) {
            return "";
        }
        for (int i = 0; i < part.length(); i++) {
            int value = Integer.parseInt(String.valueOf(part.charAt(i)));
            String number = upperNumber[value];
            String unit = cashUnitLeft[position - 1 - i];
            result = result.concat(number).concat(unit);
        }
        //把单位'亿','万','元'放到最后替换
        result = result.replaceAll("零仟", "零").replaceAll("零佰", "零")
                .replaceAll("零拾", "零").replaceAll("零零", "零")
                .replaceAll("零零", "零").replaceAll("零亿", "亿")
                .replaceAll("零万", "万").replaceAll("零元", "元");

        return result;
    }

    public static void main(String[] args) {
        BigDecimal bigDecimal = new BigDecimal("123456789.98");
        String convert = convert(bigDecimal);
        System.out.println(convert);
    }
}

